package org.kjtc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.kjtc.entity.ChargingStation;
import org.kjtc.entity.Equipment;
import org.kjtc.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author     ：Yellowong
 * @ Date       ：Created in 13:38 2018/12/10
 * @ Description：
 */
@Controller
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    @RequestMapping("/equipment")
    public String equipmentPage() {
        return "equipment";
    }

    @RequestMapping("/equipmentStatus")
    public String equipmentStatusPage() {
        return "equipmentStatus";
    }

    /**
     * 查询充电桩信息档案
     * @param chargingStationID 查询条件
     * @param equipmentType 查询条件
     * @param pageNumber 当前页数
     * @param pageSize 每页显示记录数
     * @return
     */
    @RequestMapping("/getEquipment")
    @ResponseBody
    public Object getEquipmentList(Equipment equipment,
                                   @RequestParam(value = "chargingStationID") String chargingStationID,
                                   @RequestParam(value = "equipmentType") int equipmentType,
                                   @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                                   @RequestParam(value = "pageSize", defaultValue =  "10") Integer pageSize) {

        if(!StringUtils.isEmpty(chargingStationID))
            equipment.setChargingStationID(chargingStationID);
        if(equipmentType >= 0)
            equipment.setEquipmentType(equipmentType);

        PageHelper.startPage(pageNumber, pageSize);    // 分页关键代码，只对该语句以后的第一个查询语句得到的数据进行分页

        List<Equipment> equipmentList = equipmentService.getEquipmentList(equipment);

        PageInfo pageInfo = new PageInfo(equipmentList);

        return pageInfo;
    }


    /* 统计不同设备类型的充电桩数量 0代表交流，1代表直流 */
    @RequestMapping("/equipmentStatusStatistics")
    @ResponseBody
    public List ConstructionStateStatistics() {

        return equipmentService.equipmentStatusStatistics();
    }

    /* 充电桩实时状态 */
    @RequestMapping("/getEquipmentRealTimeStatus")
    @ResponseBody
    public Object getEquipmentRealTimeStatus(
            @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
            @RequestParam(value = "pageSize", defaultValue =  "10") Integer pageSize) {

        PageHelper.startPage(pageNumber, pageSize);
        List equipmentStatusList = new ArrayList();     /* 待更改 */
        PageInfo pageInfo = new PageInfo(equipmentStatusList);

        return pageInfo;

    }

    /* 返回所有充电站名称
     * 按道理不应该写在这里（包括Mapper等）
      * 但不想动别人的代码
      * 先写了放自己这 */
    @RequestMapping("/getChargingStationName")
    @ResponseBody
    public List<Object> getChargingStationName(ChargingStation chargingStation) {

        List<Object> list = new ArrayList<Object>();
        List<ChargingStation> chargingStationNameList = equipmentService.getChargingStationName(chargingStation);

        for (ChargingStation chargingStationTemp : chargingStationNameList) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("chargingStationID", chargingStationTemp.getChargingStationID());
            map.put("chargingStationName", chargingStationTemp.getChargingStationName());
            list.add(map);
        }

        return list;
    }
}